package com.zx._02_thread.线程安全;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @version v1.0
 * @Project: knowledge
 * @Title: ThreadSafe2
 * @Description: 线程安全实例-优化性能
 * @author: zhangxue
 * @date: 2018年12月1日下午7:51:02
 * @company: 未知之地
 * @Copyright: Copyright (c) 2018
 */
public class ThreadSafe4 {

    private static int threadCount = 500;
    private static volatile AtomicInteger count = new AtomicInteger(0);
    private static ExecutorService pool = Executors.newCachedThreadPool();

    public static void main(String[] args) {
        for (int i = 0; i < threadCount; i++) {
            pool.execute(() -> {
                count.incrementAndGet();
            });
        }

        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(count);
        pool.shutdown();
    }
}
